package com.wangsd.web.face.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangsd.web.face.entity.Agent;
import com.wangsd.web.face.entity.Merchant;
import com.wangsd.web.face.entity.ServiceBusiness;
import com.wangsd.web.face.mapper.ServiceBusinessMapper;
import com.wangsd.web.face.service.AgentService;
import com.wangsd.web.face.service.ServiceBusinessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-05
 */
@Service
public class ServiceBusinessServiceImpl extends ServiceImpl<ServiceBusinessMapper, ServiceBusiness> implements ServiceBusinessService {

    @Resource
    ServiceBusinessMapper serviceBusinessMapper;

    @Autowired
    AgentService agentService;

    @Override
    public IPage<ServiceBusiness> selectList(Integer pageNum, Integer pageSize, ServiceBusiness serviceBusiness, String code) {
        Page<ServiceBusiness> page = new Page<>(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(serviceBusiness.getName())) {
            queryWrapper.like("name", serviceBusiness.getName());
        }
        if (StringUtils.isNotBlank(serviceBusiness.getShortName())) {
            queryWrapper.like("short_name", serviceBusiness.getShortName());
        }
        if (StringUtils.isNotBlank(serviceBusiness.getWxAppId())) {
            queryWrapper.like("wx_app_id", serviceBusiness.getWxAppId());
        }
        if (StringUtils.isNotBlank(serviceBusiness.getAliAppId())) {
            queryWrapper.like("ali_app_id", serviceBusiness.getAliAppId());
        }
        if (StringUtils.isNotBlank(serviceBusiness.getCode())) {
            queryWrapper.likeRight("code", serviceBusiness.getCode());
        }
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        IPage<ServiceBusiness> ServiceBusinessIPage = serviceBusinessMapper.selectPage(page, queryWrapper);
        return ServiceBusinessIPage;
    }

    @Override
    @Transactional
    public void addOrUpdate(ServiceBusiness serviceBusiness) {
        if (serviceBusiness.getId() != null) {
            serviceBusinessMapper.updateById(serviceBusiness);
        } else {
            serviceBusinessMapper.insert(serviceBusiness);

            //保存检索CODE
            serviceBusiness.setCode(serviceBusiness.getId() + "");
            serviceBusinessMapper.updateById(serviceBusiness);

            /**
             * 创建服务商时，默认创建代理商
             */
            Agent agent = new Agent();
            agent.setBusinessId(serviceBusiness.getId());
            agent.setName(serviceBusiness.getName());
            agentService.addOrUpdate(agent);
        }
    }

    @Override
    public List<Map> selectAllByUserCode(String code) {
        List<Map> resultList = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        queryWrapper.eq("deleted","0");
        List<ServiceBusiness> list = serviceBusinessMapper.selectList(queryWrapper);
        for (ServiceBusiness sb : list) {
            Map map = new HashMap();
            map.put("id", sb.getId());
            map.put("name", sb.getName());
            map.put("code", sb.getCode());
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public List<ServiceBusiness> selectAllByCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        queryWrapper.eq("deleted", "0");
        List<ServiceBusiness> list = serviceBusinessMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<ServiceBusiness> selectByCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.eq("code", code);
        }
        queryWrapper.eq("deleted", "0");
        List<ServiceBusiness> list = serviceBusinessMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public ServiceBusiness selectBusinessByCode(String code) {
        String[] ids = code.split("-");
        if (ids != null && ids.length >= 3) {
            Long businessId = Long.valueOf(ids[0]);
            return serviceBusinessMapper.selectById(businessId);
        }
        return null;
    }
}
